<html>
    <head>
        <title>Itowns - collada</title>

        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">
        <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
    </head>
    <body>
        <div id="viewerDiv"></div>
        <script src="js/GUI/GuiTools.js"></script>
        <script src="../dist/itowns.js"></script>
        <script src="js/ThreeLoader.js"></script>
        <script src="js/GUI/LoadingScreen.js"></script>
        <script src="../dist/debug.js"></script>
        <script type="text/javascript">
            // # Simple Globe viewer

            // Define initial camera position
            // Coordinate can be found on https://www.geoportail.gouv.fr/carte
            // setting is "coordonnée geographiques en degres decimaux"

            // Position near Gerbier mountain.
            var placement = {
                coord: new itowns.Coordinates('EPSG:4326', 4.21655, 44.84415),
                range: 500,
                heading: 180,
                tilt: 60,
            }

            // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
            var viewerDiv = document.getElementById('viewerDiv');

            // Instanciate iTowns GlobeView*
            var view = new itowns.GlobeView(viewerDiv, placement);

            var menuGlobe = new GuiTools('menuDiv', view);

            setupLoadingScreen(viewerDiv, view);

            function addLayerCb(layer) {
                view.addLayer(layer).then(menuGlobe.addLayerGUI.bind(menuGlobe));
            }

            // Add one imagery layer to the scene
            // This layer is defined in a json file but it could be defined as a plain js
            // object. See Layer* for more info.
            itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then(function _(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ColorLayer(config.id, config);
                view.addLayer(layer).then(menuGlobe.addLayerGUI.bind(menuGlobe));
            });
            // Add two elevation layers.
            // These will deform iTowns globe geometry to represent terrain elevation.
            function addElevationLayerFromConfig(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ElevationLayer(config.id, config);
                view.addLayer(layer).then(menuGlobe.addLayerGUI.bind(menuGlobe));
            }
            itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig);
            itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);

            // ThreeLoader can load each format proposed in ThreeJs examples loaders : https://github.com/mrdoob/three.js/tree/dev/examples/js/loaders
            var promiseCollada = ThreeLoader.load('Collada', 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/models/collada/building.dae')
            .then(collada => {
                var model = collada.scene;

                // building coordinate
                var coord = new itowns.Coordinates('EPSG:4326', 4.2165, 44.844, 1417);
                var colladaID = view.mainLoop.gfxEngine.getUniqueThreejsLayer();

                model.position.copy(coord.as(view.referenceCrs));
                // align up vector with geodesic normal
                model.lookAt(model.position.clone().add(coord.geodesicNormal));
                // user rotate building to align with ortho image
                model.rotateZ(-Math.PI * 0.2);
                model.scale.set(1.2, 1.2, 1.2);

                // set camera's layer to do not disturb the picking
                model.traverse(function _(obj) { obj.layers.set(colladaID); });
                view.camera.camera3D.layers.enable(colladaID);

                // update coordinate of the mesh
                model.updateMatrixWorld();

                view.scene.add(model);
                view.notifyChange();
            });
        </script>
    </body>
</html>
